﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YLPublicService.TransferModel;
using System.IO;

namespace YLClient.UserControls.TemplateControls
{
    /// <summary>
    /// Interaction logic for RequireAttachment.xaml
    /// </summary>
    public partial class RequireAttachment : UserControl
    {
        public Color Red = (Color)ColorConverter.ConvertFromString("#FFFF9D9D");
        public Color Green = (Color)ColorConverter.ConvertFromString("#FFC7FFFF");
        public RequireAttachment(TemplateItem.TemplateDetial.RequireAttchments.Item item)
        {
            InitializeComponent();
            Background = new SolidColorBrush(Red);
            Item = item;
            labName.Content = item.mMemo;
            IsSetValue = false;
        }

        public TemplateItem.TemplateDetial.RequireAttchments.Item Item { get; set; }
        public string FilePath { get; set; }
        public bool IsSetValue { get; private set; }

        private void loadImage()
        {
            if (Item.mValue == "")
            {
                return;
            }
            img.Source = new BitmapImage(new Uri(FilePath));
            Background = new SolidColorBrush(Green);
        }
        private bool checkDropFile(string file)
        {
            return Common.getFileExtension(file).ToLower() == Item.mDataType.ToLower();
        }
        private void UserControl_Drop_1(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Link;
                String[] pathes = e.Data.GetData(DataFormats.FileDrop) as string[];
                if (checkDropFile(pathes[0]))
                {
                    var fileName = Common.getFileName(pathes[0]);
                    Item.mValue = fileName;
                    IsSetValue = true;
                    txtbValue.Text = fileName;
                    FilePath = pathes[0];
                    loadImage();
                }
            }
        }
        public override string ToString()
        {
            return Item.ToString();
        }
    }
}
